﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tester.Models;
using Tester.Views;

namespace Tester.ViewModels;
internal class OptionViewModels : IViewModels
{
    public List<IWidget> Widgets { get ; set; }
    public CommonConfig CommonConfig { get; set; }

    public OptionViewModels()
    {
        CommonConfig = new();
        try
        {
            CommonConfig = Models.ReadAndWrite.Deserialize(Models.ReadAndWrite.FileRead());
        }
        catch (Exception ex)
        {
            DisplayAlert("提示", "未找到配置文件或配置文件不正确。\n将进行初始化。", "OK");
            InitConfig();
        }
        if (CommonConfig == null)
        {
            DisplayAlert("提示", "未找到配置文件或配置文件不正确。\n将进行初始化。", "OK");
            InitConfig();
        }

        Widgets = new List<IWidget>();

        InitializeWidget();
    }
    public void InitializeWidget()
    {
        Widgets.Add(new CommonWidget { Header = "访问作者的Gitee", Mode = 0, Description = "\ue76c GoTo", EditButtonCommand = new Command(Navito) });
        Widgets.Add(new CommonWidget { Header = "版本号", Description = VersionTracking.CurrentVersion, Mode = 1 });
    }

    private void Navito()
    {
        Navigate.Execute("Navigate", typeof(Tester.Views.SubViews.BrowserPage));
    }

    private void InitConfig()
    {
        CommonConfig = new();
        CommonConfig.themes.Add("跟随系统");
        CommonConfig.themes.Add("浅色");
        CommonConfig.themes.Add("深色");
        Models.ReadAndWrite.Write(CommonConfig);
        try
        {
            CommonConfig = Models.ReadAndWrite.Deserialize(Models.ReadAndWrite.FileRead());
        }
        catch (Exception ex)
        {
            DisplayAlert("提示", "配置文件不正确。", "OK");
            throw new Exception();
        }
    }

    private void DisplayAlert(string title, string details, string accept)
    {
        Models.Message.Show("ShowDialog", title, details, accept);
    }
}
